package com.faithbj.printer.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.util.Properties;

import org.apache.commons.logging.Log;

import com.faithbj.printer.logger.Logger;

public class PropertiesUtil {
	
	private static final String FILE_PATH="D:/printer/sysConfig.properties";
//	private static final String FILE_PATH="D:/eclipse/workspace/printer2/src/sysConfig.properties";
	public static  Properties propUtil = new Properties();
	
	@Logger private Log log;
	
	/**
	 * 写入properties信息
	 * @param parameterName
	 * @param parameterValue
	 */
	public static void writeProperties(String parameterName,String parameterValue) {
		try {
			InputStream fis = new FileInputStream(FILE_PATH);
			propUtil.load(fis);
			OutputStream fos = new FileOutputStream(FILE_PATH);
			propUtil.setProperty(parameterName, parameterValue);
			propUtil.store(fos, "Update '" + parameterName + "' value");
		} 
		catch (IOException e) {
			System.out.println("修改配置文件异常：key："+parameterName+",value:" + parameterValue+ " value error"+e.getMessage());
			System.exit(0);
		}
	} 	
	

	/**
	 *  根据key读取value
	 * @param key
	 * @return
	 */
	public static String readValue(String key) {
		String value = "";
		try {
		InputStream in = new BufferedInputStream(new FileInputStream(FILE_PATH));
		propUtil.load(in);
		value = propUtil.getProperty(key);
		return value;
		} 
		catch (Exception e) {
		System.out.println("根据key读取value异常：key："+key+",value:" + value+"，异常信息"+e.getMessage());
		return null;
		}
	}
	
}
